<?php
class IcqContext {

    private $_uin;

    private $_password;

    private $_authorizationRequests = array();

    private $_messages = array();

    private $_searchRequests = array();

    private $_searchResults = array();

    public function IcqContext($uin, $password) {
        $this->_uin = $uin;
        $this->_password = $password;
    }

    public function getUin() {
        return $this->_uin;
    }

    public function getPassword() {
        return $this->_password;
    }

    /**
     * Adds one authorization request to the queue
     * @param IcqAuthorizationRequest $value
     **/
    public function addAuthorizationRequest($value) {
        $this->_authorizationRequests[] = $value;
    }

    /**
     * Deletes an authorization request at the given index
     * @param Integer $index
     **/
    public function deleteAuthorizationRequest($index) {
        unset($this->_authorizationRequests[$index]);
        $this->_authorizationRequests = array_values($this->_authorizationRequests);
    }

    /**
     * @return Array of all authorization requests
     **/
    public function getAuthorizationRequests() {
        return $this->_authorizationRequests;
    }

    /**
     * Adds one message to the queue
     * @param IcqMessage $message
     **/
    public function addMessage($message) {
        $this->_messages[] = $message;
    }

    /**
     * Deletes a message at the given index
     * @param integer $index
     **/
    public function deleteMessage($index) {
        unset($this->_messages[$index]);
        $this->_messages = array_values($this->_messages);
    }

    /**
     * @return Array All messages in the queue
     **/
    public function getMessages() {
        return $this->_messages;
    }

    /**
     * Clears all messages in the queue
     **/
    public function clearMessages() {
        $this->_messages = array();
    }

    /**
     * Adds one searh request to the queue
     * @param IcqSearchRequest $searchRequest
     **/
    public function addSearchRequest($searchRequest) {
        $this->_searchRequests[] = $searchRequest;
    }

    /**
     * Deletes a search request at the given index
     * @param integer $index
     **/
    public function deleteSearchRequest($index) {
        unset($this->_searchRequests[$index]);
        $this->_searchRequests = array_values($this->_searchRequests);
    }

    /**
     * @return Array All search requests in the queue
     **/
    public function getSearchRequests() {
        return $this->_searchRequests;
    }

    /**
     * Adds one searh result to the context
     * @param IcqAccount $searchResult
     **/
    public function addSearchResult($searchResult) {
        $this->_searchResults[] = $searchResult;
    }

    /**
     * Adds a range of search results to the context
     * @param Array $searchResultRange
     **/
    public function addSearchResultRange($searchResultRange) {

        // Delete all search results which are already in the context from
        // the range
        foreach ($searchResultRange as $rangeKey => $rangeItem) {
            foreach ($this->_searchResults as $storedItem) {
                if ($rangeItem->uin == $storedItem->uin) {
                    unset($searchResultRange[$rangeKey]);
                }
            }
        }

        // Merge range and old results
        $this->_searchResults = array_merge($this->_searchResults, $searchResultRange);
    }

    /**
     * Deletes a search result at the given index
     * @param integer $index
     **/
    public function deleteSearchResult($index) {
        unset($this->_searchResults[$index]);
        $this->_searchResults = array_values($this->_searchResults);
    }

    /**
     * @return Array All search results in the context
     **/
    public function getSearchResults() {
        return $this->_searchResults;
    }

    /**
     * Clears all search results in the queue
     **/
    public function clearSearchResults() {
        $this->_searchResults = array();
    }

    /**
     * Loads an ICQ context from the given file
     *
     * @param String $fileName
     * @return IcqContext
     **/
    public static function load($fileName) {
        if (is_file($fileName)) {
            $content = file_get_contents($fileName);
            if (!empty($content)) {
                return unserialize($content);
            }
        }

        return null;
    }

    public static function save($fileName, $value) {
        file_put_contents($fileName, serialize($value));
    }
}
?>